package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.shop.ShopCollectMapper;
import com.ztgf.shopping.mapper.shop.ShopInfoMapper;
import com.ztgf.shopping.models.bo.shop.ShopCollectBO;
import com.ztgf.shopping.models.bo.shop.ShopCollectBatchSaveBO;
import com.ztgf.shopping.models.bo.shop.ShopCollectSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopCollectDTO;
import com.ztgf.shopping.models.service.shop.IShopCollectService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.models.vo.shop.ShopCollectVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 用户收藏  服务实现类
 * </p>
 *
 * @author zhanghualang
 * @since 2020-05-26
 */
@DubboService
@Lazy
public class IShopCollectServiceImpl extends ServiceImpl<ShopCollectMapper, ShopCollectDTO> implements IShopCollectService {
    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private ICommodityInfoService iCommodityInfoService;

    @Autowired
    private ShopInfoMapper shopInfoMapper;



    @Override
    public boolean add(ShopCollectSaveBO shopCollectSaveBO) {
        ShopCollectDTO shopCollectDTO = new ShopCollectDTO();
        BeanUtils.copyProperties(shopCollectSaveBO,shopCollectDTO);
        Long id = ymlConfig.getPkId();
        shopCollectDTO.setId(id);
        return this.insert(shopCollectDTO);
    }

    @Override
    public Page<ShopCollectVO> selectPageVO(ShopCollectBO shopCollectBO) {
        ArrayList<ShopCollectVO> shopCollectVOList = new ArrayList<ShopCollectVO>();
        Wrapper<ShopCollectDTO> wrapper = new EntityWrapper<>();

        if(shopCollectBO.getCollectShopId() != null){
            wrapper.eq("collect_shop_id", shopCollectBO.getCollectShopId());
        }

        if(shopCollectBO.getType() != null){
            wrapper.eq("type", shopCollectBO.getType());
        }

        wrapper.eq("status", 1); // 前端只能查出正常
        wrapper.orderBy("create_time", false);//降序

        Page<ShopCollectDTO> page = new Page<>(shopCollectBO.getPageNo(), shopCollectBO.getPageSize());
        page = this.selectPage(page, wrapper);

        List<ShopCollectDTO> shopCollectDTOList =  page.getRecords();

        for (ShopCollectDTO dto : shopCollectDTOList){
            ShopCollectVO shopCollectVO = new ShopCollectVO();
            BeanUtils.copyProperties(dto,shopCollectVO);
            if (dto.getCommodityId() != null){
                shopCollectVO.setCommodityPriceVO(iCommodityInfoService.getByIdOtherInterface(dto.getCommodityId()));
                shopCollectVOList.add(shopCollectVO);
            }else if(dto.getShopId() != null){
                shopCollectVO.setShopInfoDTO(shopInfoMapper.selectById(dto.getShopId()));
                shopCollectVOList.add(shopCollectVO);
            }
        }

        Page<ShopCollectVO> voPage = new Page<>(shopCollectBO.getPageNo(), shopCollectBO.getPageSize());
        voPage.setRecords(shopCollectVOList);
        voPage.setTotal(page.getTotal());
        voPage.setOffset(page.getOffset());
        return voPage;
    }

    @Override
    public Integer getBeCollectShopCount(Long shopId) {
        Wrapper<ShopCollectDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_id", shopId);
        return this.selectCount(wrapper);
    }

    public Integer getCollectShopCountByShopId(Long shopId) {
        Wrapper<ShopCollectDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("collect_shop_id", shopId);
        return this.selectCount(wrapper);
    }

    @Override
    public Integer getBeCollectCommodityCount(Long commodityId) {
        Wrapper<ShopCollectDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("commodity_id", commodityId);
        return this.selectCount(wrapper);
    }

    @Override
    public ShopCollectDTO getByShop(ShopCollectSaveBO insertSaveBO) {
        Wrapper<ShopCollectDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("collect_shop_id", insertSaveBO.getCollectShopId());
        //类型：0商品、1店铺
        if (insertSaveBO.getType() == 0){
            wrapper.eq("commodity_id", insertSaveBO.getCommodityId());
        }else if (insertSaveBO.getType() == 1){
            wrapper.eq("shop_id", insertSaveBO.getShopId());
        }

        return this.selectOne(wrapper);
    }

    @Override
    public boolean updateByShop(ShopCollectSaveBO insertSaveBO) {
        ShopCollectDTO dto = new ShopCollectDTO();
        dto.setCollectShopId(insertSaveBO.getCollectShopId());

        //类型：0商品、1店铺
        if (insertSaveBO.getType() == 0){
            dto.setCommodityId(insertSaveBO.getCommodityId());

        }else if (insertSaveBO.getType() == 1){
            dto.setShopId(insertSaveBO.getShopId());
        }

        Wrapper<ShopCollectDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("collect_shop_id", insertSaveBO.getCollectShopId());
        //类型：0商品、1店铺
        if (insertSaveBO.getType() == 0){
            wrapper.eq("commodity_id", insertSaveBO.getCommodityId());
        }else if (insertSaveBO.getType() == 1){
            wrapper.eq("shop_id", insertSaveBO.getShopId());
        }

        return this.update(dto, wrapper);
    }

    @Override
    public boolean addBatch(ShopCollectBatchSaveBO insertSaveBO) {
        //类型：0商品、1店铺
        if (insertSaveBO.getType() == 0) {
            String commodityIds = insertSaveBO.getCommodityIds();
            String[] arr = commodityIds.split(",");
            for (String commodityId : arr){
                //去重
                ShopCollectSaveBO shopCollectSaveBO = new ShopCollectSaveBO();
                shopCollectSaveBO.setCollectShopId(insertSaveBO.getCollectShopId());
                shopCollectSaveBO.setCommodityId(Long.valueOf(commodityId));
                shopCollectSaveBO.setType(insertSaveBO.getType());
                boolean flag = updateByShop(shopCollectSaveBO);
                if (flag){
                    continue;
                }

                ShopCollectDTO shopCollectDTO = new ShopCollectDTO();
                shopCollectDTO.setId(ymlConfig.getPkId());
                shopCollectDTO.setCommodityId(Long.valueOf(commodityId));
                shopCollectDTO.setType(insertSaveBO.getType());
                shopCollectDTO.setCollectShopId(insertSaveBO.getCollectShopId());
                this.insert(shopCollectDTO);
            }
        }else{
            String shopIds = insertSaveBO.getShopIds();
            String[] arr = shopIds.split(",");
            for (String shopId : arr){
                //去重
                ShopCollectSaveBO shopCollectSaveBO = new ShopCollectSaveBO();
                shopCollectSaveBO.setCollectShopId(insertSaveBO.getCollectShopId());
                shopCollectSaveBO.setShopId(Long.valueOf(shopId));
                shopCollectSaveBO.setType(insertSaveBO.getType());
                boolean flag = updateByShop(shopCollectSaveBO);
                if (flag){
                    continue;
                }

                ShopCollectDTO shopCollectDTO = new ShopCollectDTO();
                shopCollectDTO.setId(ymlConfig.getPkId());
                shopCollectDTO.setShopId(Long.valueOf(shopId));
                shopCollectDTO.setType(insertSaveBO.getType());
                shopCollectDTO.setCollectShopId(insertSaveBO.getCollectShopId());
                this.insert(shopCollectDTO);
            }
        }

        return true;
    }

    @Override
    public boolean deleteByBo(ShopCollectSaveBO bo) {
        Wrapper<ShopCollectDTO> wrapper = new EntityWrapper<>();
        //类型：0商品、1店铺
        if (bo.getType() == 0){
            wrapper.eq("commodity_id", bo.getCommodityId());
        }else if(bo.getType() == 1){
            wrapper.eq("shop_id",bo.getShopId());
        }
        wrapper.eq("collect_shop_id", bo.getCollectShopId());

        ShopCollectDTO dto = this.selectOne(wrapper);

        if (dto == null){
            return false;
        }

        return this.deleteById(dto.getId());
    }

}
